<?php
declare (strict_types=1);

namespace app\mall\model;

use app\mall\enum\FreightTypeEnum;
use app\mall\model\traits\EnableTrait;
use think\Model;

/**
 * @mixin Model
 * @property FreightTypeEnum $type_enum
 */
class MallFreightTemplate extends MallBase
{
    use EnableTrait;

    public function setAreaConfAttr($value, $data)
    {
        return $value ? json_encode($value, JSON_UNESCAPED_UNICODE) : '{}';
    }

    public function getAreaConfAttr($value, $data)
    {
        return $value ? json_decode($value, true) : [];
    }

    public function setAreaSpecialConfAttr($value, $data)
    {
        return $value ? json_encode($value, JSON_UNESCAPED_UNICODE) : '{}';
    }

    public function getAreaSpecialConfAttr($value, $data)
    {
        return $value ? json_decode($value, true) : [];
    }


    public static function buildFreightOptions($hasAll = false)
    {
        return static::buildOptions('id', 'name', $hasAll);
    }

    /**
     * 获取发货类型名称
     * @param $key
     * @param $data
     * @return string
     */
    public function getTypeTextAttr(): string
    {
        return $this->type_enum->getZhName();
    }

    public function getTypeEnumAttr($key, $data): FreightTypeEnum
    {
        return new FreightTypeEnum($data['type']);
    }
}
